package game.module.qzmj.logic;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import game.module.qzmj.bean.QzmjRoom;

public class QzmjDataManager {

	private static Logger logger = LoggerFactory.getLogger(QzmjDataManager.class);

	static class SingletonHolder {
		static QzmjDataManager instance = new QzmjDataManager();
	}

	public static QzmjDataManager getInstance() {
		return SingletonHolder.instance;
	}

	private Map<String, QzmjRoom> roomMap = new ConcurrentHashMap<>();

	private Map<Integer, String> playerRoomMap = new ConcurrentHashMap<>();

	public boolean isPlayerInRoom(int playId) {
		return playerRoomMap.containsKey(playId);
	}

	public void removeRoom(String roomUuid) {
		QzmjRoom mjRoom = roomMap.get(roomUuid);
		if (mjRoom == null) {
			return;
		}
		List<Integer> playerList = mjRoom.getPlayerList();
		for (Integer aPlayerId : playerList) {
			playerRoomMap.remove(aPlayerId);
		}
		playerList.clear();
		roomMap.remove(roomUuid);
	}

	public boolean hasRoomUuid(String roomUuid) {
		return roomMap.containsKey(roomUuid);
	}

	public void addRoom(QzmjRoom mjRoom) {
		roomMap.put(mjRoom.getUuid(), mjRoom);
		playerRoomMap.put(mjRoom.getPlayerList().get(0), mjRoom.getUuid());
	}

	public void removePlayer(int playerId) {
		playerRoomMap.remove(playerId);
	}
	public void addPlayer(int playerId, String roomId) {
		playerRoomMap.put(playerId, roomId);
	}

	public boolean checkRoomExist(String room_id) {
		return roomMap.containsKey(room_id);
	}

	public QzmjRoom getMjRoom(String room_id) {
		return roomMap.get(room_id);
	}

	public QzmjRoom getMjRoomByPlayerId(int playerId) {
		String roomId = playerRoomMap.get(playerId);
		if (roomId == null) {
			return null;
		}
		return roomMap.get(roomId);
	}

	public int getOnlineSize() {
		int ret = 0;
		for (QzmjRoom aRoom : roomMap.values()) {
			ret += aRoom.getPlayerList().size();
		}
		return ret;
	}

}
